package com.svs.framework.util;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import javax.imageio.ImageIO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ImageUtil {
	
	public static final String SLASH_ENCODE = "%2F" ;
	public static final String SLASH_ENCODE_STR = "/" ;
	public static final String COLON_ENCODE = "%3A" ;
	public static final String COLON_ENCODE_STR = ":" ;
	
	
	static Logger logger = LoggerFactory.getLogger(ImageUtil.class);


	public static byte[] getSmallImage(byte[] bytes, int width) throws Exception {
		ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
		BufferedImage src = ImageIO.read(bis);
		int height = src.getHeight() * width / src.getWidth();
		Image image = src.getScaledInstance(width, height, 4);
		BufferedImage tag = new BufferedImage(width, height, 1);
		Graphics g = tag.getGraphics();
		g.drawImage(image, 0, 0, (ImageObserver) null);
		g.dispose();
		ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
		ImageIO.write(tag, "JPEG", dataStream);
		byte[] data = dataStream.toByteArray();
		return data;
	}

	public static byte[] zoomSizeImage(byte[] bytes, double zoomRatio) throws Exception {
		ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
		BufferedImage src = ImageIO.read(bis);
		int width = (new Double((double) src.getWidth((ImageObserver) null) * zoomRatio)).intValue();
		int height = (new Double((double) src.getHeight((ImageObserver) null) * zoomRatio)).intValue();
		Image image = src.getScaledInstance(width, height, 4);
		BufferedImage tag = new BufferedImage(width, height, 1);
		Graphics g = tag.getGraphics();
		g.drawImage(image, 0, 0, (ImageObserver) null);
		g.dispose();
		ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
		ImageIO.write(tag, "JPEG", dataStream);
		byte[] data = dataStream.toByteArray();
		return data;
	}
	
}